/*
 Aion-core by vegar
 */
package com.aionengine.gameserver.skillengine.effect;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.controllers.observer.ObserverType;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.stats.container.CreatureLifeStats;
import com.aionengine.gameserver.network.aion.serverpackets.SM_ATTACK_STATUS.LOG;
import com.aionengine.gameserver.network.aion.serverpackets.SM_ATTACK_STATUS.TYPE;
import com.aionengine.gameserver.skillengine.model.Effect;
import com.aionengine.gameserver.skillengine.model.Skill;
import com.aionengine.gameserver.skillengine.model.SkillSubType;
import com.aionengine.gameserver.skillengine.model.SkillType;
import com.aionengine.gameserver.utils.ThreadPoolManager;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

/**
 * @author ViAl
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MagicCounterAtkEffect")
public class MagicCounterAtkEffect extends EffectTemplate {

    @XmlAttribute
    protected int maxdmg;

    //TODO bosses are resistent to this?
    @Override
    public void applyEffect(Effect effect) {
        effect.addToEffectedController();
    }

    @Override
    public void startEffect(final Effect effect) {
        final Creature effector = effect.getEffector();
        final Creature effected = effect.getEffected();
        final CreatureLifeStats<? extends Creature> cls = effect.getEffected().getLifeStats();
        ActionObserver observer = new ActionObserver(ObserverType.SKILLUSE) {

            @Override
            public void skilluse(final Skill skill) {
                ThreadPoolManager.getInstance().schedule(new Runnable() {

                    @Override
                    public void run() {
                        if (skill.getSkillTemplate().getType() == SkillType.MAGICAL
                                && skill.getSkillTemplate().getSubType() == SkillSubType.ATTACK) {
                            if ((int) (cls.getMaxHp() / 100f * value) <= maxdmg)
                                effected.getController().onAttack(effector, effect.getSkillId(), TYPE.DAMAGE,
                                        (int) (cls.getMaxHp() / 100f * value), true, LOG.REGULAR);
                            else
                                effected.getController().onAttack(effector, maxdmg, true);
                        }
                    }

                }, 0);

            }

        };

        effect.setActionObserver(observer, position);
        effected.getObserveController().addObserver(observer);
    }

    @Override
    public void endEffect(Effect effect) {
        ActionObserver observer = effect.getActionObserver(position);
        if (observer != null)
            effect.getEffected().getObserveController().removeObserver(observer);
    }

}